package com.yanqu.road.dao.impl.activity.flowerstudy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.flowerstudy.data.cross.CrossFlowerStudyUserExamData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FlowerStudyCrossExamUserDataDaoImpl extends TempDataObjectDao<CrossFlowerStudyUserExamData> {

    public static String getCrossFlowerStudyUserExamDataMapSql = "select * from `t_c_flower_study_activity_user_exam` where `activityId`=?";
    public Map<Long,CrossFlowerStudyUserExamData> getCrossFlowerStudyUserExamDataMap(int activityId) {
        Map<Long,CrossFlowerStudyUserExamData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFlowerStudyUserExamDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFlowerStudyUserExamData data = parse(rs);
                map.put(data.getUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getCrossFlowerStudyUserExamDataSql = "select * from `t_c_flower_study_activity_user_exam` where `userId`=? and `activityId`=?";
    public CrossFlowerStudyUserExamData getCrossFlowerStudyUserExamDataMap(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFlowerStudyUserExamDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossFlowerStudyUserExamDataSql = "update `t_c_flower_study_activity_user_exam` set serverId=?,nowRoomId=?,localExamScore=?,provinceExamScore=?,palaceExamScore=?,localExamTime=?,provinceExamTime=?,palaceExamTime=?,hasQualificationExamSession=?,todaySignUpCount=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossFlowerStudyUserExamData crossFlowerStudyUserExamData) {
        crossFlowerStudyUserExamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyUserExamData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossFlowerStudyUserExamData.getNowRoomId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getLocalExamScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getProvinceExamScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getPalaceExamScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getLocalExamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getProvinceExamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getPalaceExamTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyUserExamData.getHasQualificationExamSession()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFlowerStudyUserExamData.getTodaySignUpCount())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyUserExamData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getUserId()));

        if (getDb().executeNoneQuery(updateCrossFlowerStudyUserExamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFlowerStudyUserExamDataSql = "insert into `t_c_flower_study_activity_user_exam`(activityId,userId,serverId,nowRoomId,localExamScore,provinceExamScore,palaceExamScore,localExamTime,provinceExamTime,palaceExamTime,hasQualificationExamSession,todaySignUpCount) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossFlowerStudyUserExamData crossFlowerStudyUserExamData) {
        crossFlowerStudyUserExamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyUserExamData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyUserExamData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossFlowerStudyUserExamData.getNowRoomId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getLocalExamScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getProvinceExamScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getPalaceExamScore()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getLocalExamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getProvinceExamTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyUserExamData.getPalaceExamTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyUserExamData.getHasQualificationExamSession()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFlowerStudyUserExamData.getTodaySignUpCount())));
        if (getDb().executeNoneQuery(insertCrossFlowerStudyUserExamDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossFlowerStudyUserExamData parseData(ResultSet rs) throws SQLException {
        CrossFlowerStudyUserExamData crossFlowerStudyUserExamData = new CrossFlowerStudyUserExamData();
        crossFlowerStudyUserExamData.setUserId(rs.getLong("userId"));
        crossFlowerStudyUserExamData.setActivityId(rs.getInt("activityId"));
        crossFlowerStudyUserExamData.setServerId(rs.getInt("serverId"));
        crossFlowerStudyUserExamData.setNowRoomId(rs.getString("nowRoomId"));
        crossFlowerStudyUserExamData.setLocalExamScore(rs.getLong("localExamScore"));
        crossFlowerStudyUserExamData.setProvinceExamScore(rs.getLong("provinceExamScore"));
        crossFlowerStudyUserExamData.setPalaceExamScore(rs.getLong("palaceExamScore"));
        crossFlowerStudyUserExamData.setLocalExamTime(rs.getLong("localExamTime"));
        crossFlowerStudyUserExamData.setProvinceExamTime(rs.getLong("provinceExamTime"));
        crossFlowerStudyUserExamData.setPalaceExamTime(rs.getLong("palaceExamTime"));
        crossFlowerStudyUserExamData.setHasQualificationExamSession(rs.getInt("hasQualificationExamSession"));
        crossFlowerStudyUserExamData.setTodaySignUpCount(JSON.parseObject(rs.getString("todaySignUpCount"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));

        return crossFlowerStudyUserExamData;
    }

}
